package ru.silviasaint.fridge.client.widgets.component;

import java.util.ArrayList;
import java.util.List;

import ru.silviasaint.fridge.shared.bean.SkinnySubstance;

import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;

public class ImageSuggest extends MultiWordSuggestOracle{
	private List<SkinnySubstance> elements;
	public ImageSuggest(){
		super();
		elements = new ArrayList<SkinnySubstance>();
	}
	public void add(SkinnySubstance substance) {
		elements.add(substance);
		super.add(substance.toString());
	}

	@Override
	public void clear() {
		elements.clear();
		super.clear();
	}
	@Override
	protected MultiWordSuggestion createSuggestion(String replacementString,
			String displayString) {
		SafeHtmlBuilder accum = new SafeHtmlBuilder();

		for (SkinnySubstance substance : elements){
			if (substance.toString().contains(replacementString)){
				Image image = new Image();
				image.setUrl("/" + "data/" + substance.getClass().getName().toLowerCase() + "/" + substance.getId());
				accum.appendHtmlConstant(image.toString());
			}
		}

		accum.appendHtmlConstant(displayString);

		return super.createSuggestion(replacementString, accum.toSafeHtml().asString());
	}
}
